package com.msh.frame.client.util;

import com.alibaba.fastjson.JSONObject;
import com.msh.frame.client.define.StringDef;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class FieldChangeUtil {

  public static String fieldToString(Object o) {
    if (null == o) {
      return StringDef.EMPTY;
    }
    if (o instanceof String) {
      return (String) o;
    } else if (o instanceof Number) {
      return o.toString();
    } else if (o instanceof Date) {
      return new SimpleDateFormat(StringDef.DEFAULT_DATE_TIME_FORMAT).format(o);
    } else if (o instanceof LocalDateTime) {
      return formatter.format((LocalDateTime) o);
    } else {
      return JSONObject.toJSONString(o);
    }
  }

  private static final DateTimeFormatter formatter = DateTimeFormatter
      .ofPattern(StringDef.DEFAULT_DATE_TIME_FORMAT);
}
